<template>
  <basic-dialog
    title="创建机构"
    :visible="visible"
    :loading="loading"
    :width="448"
    has-footer
    @closed="handleClose"
    @submit="handleSubmit"
  >
    <el-form
      ref="formRef"
      :model="formState"
      :rules="rules"
      require-asterisk-position="right"
      label-width="100px"
      label-position="top"
      @submit.prevent
    >
      <el-form-item label="机构名称" prop="name">
        <el-input
          v-model="formState.name"
          placeholder="请输入机构名称"
          maxlength="40"
        />
      </el-form-item>
      <el-form-item label="证书主体名称(CN)" prop="common_name">
        <el-input
          v-model.trim="formState.common_name"
          placeholder="请输入证书主体名称(CN)"
          maxlength="40"
        />
      </el-form-item>
      <el-form-item label="公司/组织" prop="organization">
        <el-input
          v-model.trim="formState.organization"
          placeholder="请输入公司/组织"
          maxlength="40"
        />
      </el-form-item>
      <el-form-item label="部门" prop="organization_uint">
        <el-input
          v-model.trim="formState.organization_uint"
          placeholder="请输入部门"
          maxlength="40"
        />
      </el-form-item>
      <el-form-item label="国家代码" prop="country">
        <el-input
          v-model.trim="formState.country"
          placeholder="请输入国家代码"
        />
      </el-form-item>
      <section class="flex items-center justify-between gap-x-4">
        <el-form-item label="省份" prop="province" class="w-1/2">
          <el-input
            v-model.trim="formState.province"
            placeholder="请输入省份"
            maxlength="40"
          />
        </el-form-item>
        <el-form-item label="城市" prop="city" class="w-1/2">
          <el-input
            v-model.trim="formState.city"
            placeholder="请输入城市"
            maxlength="40"
          />
        </el-form-item>
      </section>
      <el-form-item prop="key_type" label="密钥算法">
        <el-select v-model="formState.key_type" class="w-full">
          <el-option
            v-for="item in ALGORITHM_OPTS"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </basic-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'

import type { CreateSelfCertParams } from '@/apis/model/selfCert'
import { ALGORITHM_OPTS } from '@/config/constant/cert'
interface IProps {
  visible: boolean
  loading: boolean
}
interface IEmits {
  (event: 'update:visible', value: boolean): void
  (event: 'update:loading', value: boolean): void
  (event: 'refresh'): void
}

defineProps<IProps>()
const emit = defineEmits<IEmits>()
const formRef = ref<FormInstance>()

const formState = reactive<CreateSelfCertParams>({
  city: '',
  common_name: '',
  country: '',
  key_type: ALGORITHM_OPTS[0].value,
  name: '',
  organization: '',
  organization_uint: '',
  province: ''
})

const rules: Record<string, FormItemRule[]> = {
  name: [{ required: true, message: '请输入机构名称', trigger: 'blur' }],
  country: [{ required: true, message: '请选择国家代码', trigger: 'blur' }],
  key_type: [{ required: true, message: '请选择', trigger: 'change' }],
  common_name: [
    { required: true, message: '请输入证书主体名称(CN)', trigger: 'blur' }
  ],
  organization: [
    { required: true, message: '请输入公司/组织', trigger: 'blur' }
  ]
}
const handleClose = () => {
  formRef.value?.resetFields()
  emit('update:visible', false)
  emit('refresh')
}
async function handleSubmit() {
  await formRef.value?.validate()
  emit('update:loading', true)
  await createSelfCertApi(formState)
    .then(() => {
      handleClose()
      ToastUtils.success('操作成功')
    })
    .finally(() => emit('update:loading', false))
}
</script>

<style lang="scss" scoped></style>
